import { Component, OnInit } from '@angular/core';
import { ModelService } from "../../../../service/model.service";
import { Model } from "../../../../entity/model.model";
import { BladeContainerBase } from "../../../../shared/blade/blade-container-base";
import { BladeData } from "../../../../shared/blade/blade-data";
import { Blade } from "../../../../shared/blade/blade.model";
import { Command } from "../../../../shared/toolbar.service";
import { DialogResult } from "../../../../shared/dialog.service";
import { ModelEditComponent } from "../model-edit/model-edit.component"
import { StandardCurveComponent } from "../standard-curve/standard-curve.component";
import { StateInfoComponent } from "../state-info/state-info.component"
import { FaultAlarmComponent } from "../fault-alarm/fault-alarm.component"
import { TagMapComponent } from "../tag-map/tag-map.component";
import { StausExperListComponent } from "../../staus-exper/staus-exper-list/staus-exper-list.component";
import { EditorParameterComponent } from "../editor-parameter/editor-parameter.component"
import { FactoryService } from "../../../../service/factory.service";
import { Factory } from "../../../../entity/factory.model";
import { DeviceCategoryService } from "../../../../service/device-category.service";
import { DeviceCategory } from "../../../../entity/device-category.model";
import { ApplicationService } from "../../../../service/application.service";

@Component({
  selector: 'app-model-list',
  templateUrl: './model-list.component.html',
  styleUrls: ['./model-list.component.css']
})
export class ModelListComponent extends BladeContainerBase<Model> {

  /**
   * 风机型号集合
   * 
   * @type {Model[]}
   * @memberof ModelListComponent
   */
  models: Model[] = [];
  deviceCategorys: DeviceCategory[];

  /**
   * 当前路径
   * 
   * @type {string}
   * @memberof ModelListComponent
   */
  localhostPath: string;
  factorys: Factory[];
  constructor(
    private modelService: ModelService,
    private factoryService: FactoryService,
    private deviceCateSer: DeviceCategoryService,
    private application:ApplicationService
  ) {
    super()
  }

  ngOnInit() {
    this.commands = [
      new Command("add", "添加", "fa fa-plus", "setofkey:insert", blade => this.exeAdd(blade), blade => this.canExeAdd(blade)),
      new Command("edit", "编辑", "fa fa-edit", "setofkey:update", blade => this.exeEdit(blade), blade => this.canExeEdit(blade)),
      new Command("delete", "删除", "fa fa-trash-o", "setofkey:delete", blade => this.exeDelete(blade), blade => this.canDelete(blade)),
      new Command("standardCurve", "标准曲线", "fa fa-line-chart", "setofkey:create", blade => this.exestandardCurve(blade), blade => this.canExeEdit(blade)),
      new Command("stateInfo", "状态信息", "fa fa-circle-o-notch", "setofkey:create", blade => this.exeStateInfo(blade), blade => this.canExeEdit(blade)),
      new Command("faultAlarm", "故障报警", "fa fa-exclamation-circle", "setofkey:create", blade => this.exeFaultAlarm(blade), blade => this.canExeEdit(blade)),
      new Command("tagMap", "测点映射", "fa fa-tags", "setofkey:create", blade => this.exeTagMap(blade), blade => this.canExeEdit(blade)),
      new Command("stateJudgment", "状态判断", "fa fa-circle-o-notch", "setofkey:create", blade => this.exeStateJudgment(blade), blade => this.canExeEdit(blade)),
      new Command("editParam", "编辑参数", "fa fa-circle-o-notch", "setofkey:create", blade => this.exeEditParam(blade), blade => this.canExeEdit(blade))
    ];
    this.localhostPath = this.application.appConfigData.appRestful.split("/webapi")[0];
    const getDeviceCat = this.deviceCateSer.getDeviceCategorys();
    const getFactory = this.factoryService.getFactory();
    Promise.all([getDeviceCat, getFactory])
      .then(res => {
        if (res) {
          this.deviceCategorys = [...res[0]];
          this.factorys = [...res[1]]
        }
      })
    super.ngOnInit();

  }
  initData(): any {
    this.modelService.getModelWithFactory()
      .then(res => {
        if(!res)return false;
        this.models = [...res];
        this.bladeData.isLoading = false;
      })
      .catch(err => {
        console.log(err)
      });
  }
  exeAdd(blend: Blade): void {
    const data: BladeData = new BladeData();
    data.id = "modelGroupingCreate";
    data.title = "创建一个型号信息";
    data.subtitle = "创建一个型号信息";
    data.isClosingDisabled = false;
    const createKeyBlade = this.bladeService.createBlade(ModelEditComponent, data, true, {}, { factory: this.factorys, deviceCategory: this.deviceCategorys });
    this.bladeService.showBlade(createKeyBlade, this.currentBlade);
  }
  canExeAdd(blend: Blade): boolean {
    return true;
  }
  exeEdit(blend: Blade): void {
    if (this.currentEntity != undefined) {
      let data: BladeData = new BladeData();
      data.id = "turbaKeyEdit";
      data.title = this.currentEntity.modelName;
      let editKeyBlade = this.bladeService.createBlade(ModelEditComponent, data, false, this.currentEntity, { factory: this.factorys, deviceCategory: this.deviceCategorys });
      this.currentBlade.bladeService.showBlade(editKeyBlade, this.currentBlade);
    }
  }
  canExeEdit(blend?: Blade): boolean {
    return this.currentEntity != null;
  }
  exeDelete(blend: Blade): void {
    let selectedKeys = this.getSelected();
    if (selectedKeys.length > 0) {
      this.dialogService
        .showConfirmationDialog("确认删除", "是否要删除选中项？")
        .subscribe(dialogResult => {
          if (dialogResult == DialogResult.Ok) {
            this.currentBlade.bladeData.isLoading = true;
            this.modelService.batchDelete(selectedKeys.map(o => o.modelId))
              .then(res => {
                if (res.code == '200') {
                  super.refresh(undefined);
                }
                if (res.code == '8001') {
                  this.dialogService
                    .showConfirmationDialog("删除失败", res.message);
                }
              })
              .catch(error => this.bladeData.error = error)
              .then(res => this.bladeData.isLoading = false);
          }
        })
    }
  }
  canDelete(blend?: Blade): boolean {
    return this.getSelected().length > 0;
  }


  /**
   * 状态信息
   * 
   * @param {any} blade 
   * @memberof ModelListComponent
   */
  exeStateInfo(blade) {
    if (this.currentEntity != undefined) {
      let data: BladeData = new BladeData();
      data.id = "stateInfo";
      data.title = this.currentEntity.modelName;
      data.subtitle = "状态信息分组";
      let editKeyBlade = this.bladeService.createBlade(StateInfoComponent, data, false, this.currentEntity);
      this.currentBlade.bladeService.showBlade(editKeyBlade, this.currentBlade);
    }
  }
  /**
 * 故障信息
 * 
 * @param {any} blade 
 * @memberof ModelListComponent
 */
  exeFaultAlarm(blade) {
    if (this.currentEntity != undefined) {
      let data: BladeData = new BladeData();
      data.id = "faultAlarm";
      data.title = this.currentEntity.modelName;
      data.subtitle = "故障报警分组";
      let editKeyBlade = this.bladeService.createBlade(FaultAlarmComponent, data, false, this.currentEntity);
      this.currentBlade.bladeService.showBlade(editKeyBlade, this.currentBlade);
    }
  }
  /**
 * 测点映射
 * 
 * @param {any} blade 
 * @memberof ModelListComponent
 */
  exeTagMap(blade) {
    if (this.currentEntity != undefined) {
      let data: BladeData = new BladeData();
      data.id = "tagMap";
      data.title = this.currentEntity.modelName;
      data.subtitle = "测点映射分组";
      let editKeyBlade = this.bladeService.createBlade(TagMapComponent, data, false, this.currentEntity);
      this.currentBlade.bladeService.showBlade(editKeyBlade, this.currentBlade);
    }
  }
  /**
 * 状态判断
 * 
 * @param {any} blade 
 * @memberof ModelListComponent
 */
  exeStateJudgment(blade) {
    if (this.currentEntity != undefined) {
      let data: BladeData = new BladeData();
      data.id = "stateJudgment";
      data.title = this.currentEntity.modelName;
      data.subtitle = "状态判断";
      let editKeyBlade = this.bladeService.createBlade(StausExperListComponent, data, false, this.currentEntity, this.currentEntity);
      this.currentBlade.bladeService.showBlade(editKeyBlade, this.currentBlade);
    }
  }
  /**
   * 编辑参数
   * 
   * @param {any} blade 
   * @memberof ModelListComponent
   */
  exeEditParam(blade) {
    if (this.currentEntity != undefined) {
      let data: BladeData = new BladeData();
      data.id = "editParam";
      data.title = this.currentEntity.modelName;
      data.subtitle = "型号参数列表";
      let editKeyBlade = this.bladeService.createBlade(EditorParameterComponent, data, false, null, this.currentEntity);
      this.currentBlade.bladeService.showBlade(editKeyBlade, this.currentBlade);
    }
  }

  /**
 * 标准风功率曲线
 * 
 * @param {any} blade 
 * @memberof ModelListComponent
 */
  exestandardCurve(blade) {
    if (this.currentEntity != undefined) {
      const data: BladeData = new BladeData();
      data.id = "standardCurve";
      data.title = this.currentEntity.modelName;
      data.subtitle = "标准风功率曲线分组";
      let editKeyBlade = this.bladeService.createBlade(StandardCurveComponent, data, false, this.currentEntity);
      this.currentBlade.bladeService.showBlade(editKeyBlade, this.currentBlade);
    }
  }
  /**
* 单选
* 
* @param {any} event 
* @param {any} key 
* 
* @memberof ModelListComponent
*/
  onItemCheack(event: HTMLInputElement, key: Model) {
    key.isChecked = event.checked;
  }
  /**
   * 全选/全不选
   * 
   * @param {HTMLInputElement} event 
   * 
   * @memberof ModelListComponent
   */
  onAllCheack(event: HTMLInputElement) {
    if (this.models.length > 0) {
      for (let i = 0; i < this.models.length; i++) {
        this.models[i].isChecked = event.checked;
      }
    }
  }
  onSeletedChange(selectedKey: Model) {
    if (this.currentBlade.childrenCanDeactivate()) {
      this.currentEntity = selectedKey;
    }
  }
  /**
* 获得选中项
* 
* @returns {Setofkey[]} 
* 
* @memberof ModelListComponent
*/
  getSelected(): Model[] {
    if (this.models.length > 0) {
      let filter = this.models.filter(ref => ref.isChecked)
      return filter == null ? [] : filter;
    }
    return [];
  }

}



// WEBPACK FOOTER //
// ./src/app/module/basic/model/model-list/model-list.component.ts